package com.csw;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.mapreduce.KeyValueSortReducer;
import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles;
import org.apache.hadoop.hbase.mapreduce.SimpleTotalOrderPartitioner;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;


public class Demo10BulkLoading {
    public static class BulkLoadingMap extends Mapper<LongWritable,Text,ImmutableBytesWritable,KeyValue>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] splits = value.toString().split(",");

            String mdn = splits[0];
            String start_time = splits[1];
            // 经度
            String longitude = splits[4];
            // 维度
            String latitude = splits[5];

            String rowkey = mdn + "_" + start_time;

            KeyValue lg = new KeyValue(rowkey.getBytes(), "info".getBytes(), "lg".getBytes(), longitude.getBytes());
            KeyValue lt = new KeyValue(rowkey.getBytes(), "info".getBytes(), "lt".getBytes(), latitude.getBytes());

            context.write(new ImmutableBytesWritable(rowkey.getBytes()),lg);
            context.write(new ImmutableBytesWritable(rowkey.getBytes()),lt);
        }
    }

    public static void main(String[] args) throws Exception{
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");

        //创建Job实例
        Job job = Job.getInstance(conf);
        job.setJarByClass(Demo10BulkLoading.class);
        job.setJobName("Demo10BulkLoading");

        //设置Reduce个数
        job.setNumReduceTasks(4);

        //保证全局有序
        job.setPartitionerClass(SimpleTotalOrderPartitioner.class);

        //配置Map任务
        job.setMapperClass(BulkLoadingMap.class);
        //配置Reduce任务
        //KeyValueSortReducer  保证在每一个Reduce有序
        job.setReducerClass(KeyValueSortReducer.class);


        //输入输出路径
        FileInputFormat.addInputPath(job,new Path("/data/DIANXIN/"));
        FileOutputFormat.setOutputPath(job,new Path("/data/hfile"));


        //创建HBase连接
        Connection connection = ConnectionFactory.createConnection(conf);
        //create 'dianxin_bulk','info'
        //获取dianxin_bulk这张表
        Table dianxin_bulk = connection.getTable(TableName.valueOf("dianxin_bulk"));
        //获取dianxin_bulk这张表的region定位器
        RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf("dianxin_bulk"));

        //使用HFilOutputFormat2将输出的数据按照HFile的形式格式化
        HFileOutputFormat2.configureIncrementalLoad(job,dianxin_bulk,regionLocator);

        //等待MapReduce任务执行完成
        job.waitForCompletion(true);

        //加载HFil到 dianxin_bulk表中
        LoadIncrementalHFiles loadIncrementalHFiles = new LoadIncrementalHFiles(conf);
        loadIncrementalHFiles.doBulkLoad(new Path("/data/hfile"),connection.getAdmin(),dianxin_bulk,regionLocator);

        /**先建表
         * create 'dianxin_bulk','info'
         *
         * hadoop jar HBaseAPI-1.0-jar-with-dependencies.jar com.csw.Demo10BulkLoading
         */
    }
}
